package com.yc.spring.mvc.web;

import com.yc.spring.mvc.entity.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@Component
//控制器增强注解
//RestController 前缀表示该类的所有方法返回json数据
@RestControllerAdvice
public class ExAdvice {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Result maxUploadSizeExceededException(MaxUploadSizeExceededException ex){
        String msg="上传文件不能超过 %s ";
        msg=String.format(msg,"1MB");
        return new Result(0,msg,null);
    }

    @ExceptionHandler(Exception.class)
    public Result exception(Exception ex){
        ex.printStackTrace();
        //通知客服
        return new Result(0,"系统繁忙，稍后再试",null);
    }
}
